import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

// 路由懒加载
const LoginView = () => import('@/views/auth/LoginView.vue')
const RegisterView = () => import('@/views/auth/RegisterView.vue')
const HRDashboard = () => import('@/views/hr/DashboardView.vue')
const CandidateDashboard = () => import('@/views/candidate/DashboardView.vue')

// HR 相关页面
const CreateInterviewView = () => import('@/views/hr/CreateInterviewView.vue')
const QuestionBankView = () => import('@/views/hr/QuestionBankView.vue')
const EvaluationReportView = () => import('@/views/hr/EvaluationReportView.vue')
const PlaybackView = () => import('@/views/hr/PlaybackView.vue')
const StatisticsView = () => import('@/views/hr/StatisticsView.vue')
const ReportsView = () => import('@/views/hr/ReportsView.vue')
const InterviewManagementView = () => import('@/views/hr/InterviewManagementView.vue')
const InterviewDetailView = () => import('@/views/hr/InterviewDetailView.vue')
const InterviewManagementDetailView = () => import('@/views/hr/InterviewManagementDetailView.vue')
const ApplicationsManagementView = () => import('@/views/hr/ApplicationsManagementView.vue')

// 候选人相关页面
const InterviewSessionView = () => import('@/views/candidate/InterviewSessionView.vue')
const PracticeView = () => import('@/views/candidate/PracticeView.vue')
const SearchInterviewsView = () => import('@/views/candidate/SearchInterviewsView.vue')
const MatchAnalysisView = () => import('@/views/candidate/MatchAnalysisView.vue')
const PracticeHistoryView = () => import('@/views/candidate/PracticeHistoryView.vue')
const CandidateStatisticsView = () => import('@/views/candidate/StatisticsView.vue')
const MyInterviewsView = () => import('@/views/candidate/MyInterviewsView.vue')
const InterviewResultView = () => import('@/views/candidate/InterviewResultView.vue')
const InterviewFeedbackView = () => import('@/views/candidate/InterviewFeedbackView.vue')

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginView,
    meta: {
      title: '登录 - Sparkie Interview',
      requiresAuth: false
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: RegisterView,
    meta: {
      title: '注册 - Sparkie Interview',
      requiresAuth: false
    }
  },
  {
    path: '/hr',
    name: 'HR',
    redirect: '/hr/dashboard',
    meta: {
      requiresAuth: true,
      role: 'hr'
    },
    children: [
      {
        path: 'dashboard',
        name: 'HRDashboard',
        component: HRDashboard,
        meta: {
          title: 'HR 仪表盘 - Sparkie Interview'
        }
      },
      {
        path: 'create-interview',
        name: 'CreateInterview',
        component: CreateInterviewView,
        meta: {
          title: '创建面试 - Sparkie Interview'
        }
      },
      {
        path: 'question-bank',
        name: 'QuestionBank',
        component: QuestionBankView,
        meta: {
          title: '题库管理 - Sparkie Interview'
        }
      },
      {
        path: 'evaluation/:id',
        name: 'EvaluationReport',
        component: EvaluationReportView,
        meta: {
          title: '评估报告 - Sparkie Interview'
        }
      },
      {
        path: 'playback/:id',
        name: 'Playback',
        component: PlaybackView,
        meta: {
          title: '视频回放 - Sparkie Interview'
        }
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: StatisticsView,
        meta: {
          title: '面试统计 - Sparkie Interview'
        }
      },
      {
        path: 'reports',
        name: 'Reports',
        component: ReportsView,
        meta: {
          title: '候选人报告 - Sparkie Interview'
        }
      },
      {
        path: 'interviews',
        name: 'InterviewManagement',
        component: InterviewManagementView,
        meta: {
          title: '面试管理 - Sparkie Interview'
        }
      },
      {
        path: 'interviews/:id',
        name: 'InterviewDetail',
        component: InterviewDetailView,
        meta: {
          title: '面试详情 - Sparkie Interview'
        }
      },
      {
        path: 'interview-management/:applicationId',
        name: 'InterviewManagementDetail',
        component: InterviewManagementDetailView,
        meta: {
          title: '面试管理详情 - Sparkie Interview'
        }
      },
      {
        path: 'applications',
        name: 'ApplicationsManagement',
        component: ApplicationsManagementView,
        meta: {
          title: '申请管理 - Sparkie Interview'
        }
      }
    ]
  },
  {
    path: '/candidate',
    name: 'Candidate',
    redirect: '/candidate/dashboard',
    meta: {
      requiresAuth: true,
      role: 'candidate'
    },
    children: [
      {
        path: 'dashboard',
        name: 'CandidateDashboard',
        component: CandidateDashboard,
        meta: {
          title: '候选人首页 - Sparkie Interview'
        }
      },
      {
        path: 'interview-session/:applicationId?',
        name: 'InterviewSession',
        component: InterviewSessionView,
        meta: {
          title: '面试进行中 - Sparkie Interview'
        }
      },
      {
        path: 'practice',
        name: 'Practice',
        component: PracticeView,
        meta: {
          title: '模拟练习 - Sparkie Interview'
        }
      },
      {
        path: 'search-interviews',
        name: 'SearchInterviews',
        component: SearchInterviewsView,
        meta: {
          title: '查找面试 - Sparkie Interview'
        }
      },
      {
        path: 'match-analysis/:id',
        name: 'MatchAnalysis',
        component: MatchAnalysisView,
        meta: {
          title: '匹配度分析 - Sparkie Interview'
        }
      },
      {
        path: 'practice-history',
        name: 'PracticeHistory',
        component: PracticeHistoryView,
        meta: {
          title: '练习历史 - Sparkie Interview'
        }
      },
      {
        path: 'statistics',
        name: 'CandidateStatistics',
        component: CandidateStatisticsView,
        meta: {
          title: '面试成绩详情 - Sparkie Interview'
        }
      },
      {
        path: 'my-interviews',
        name: 'MyInterviews',
        component: MyInterviewsView,
        meta: {
          title: '我的面试 - Sparkie Interview'
        }
      },
      {
        path: 'interview-result',
        name: 'InterviewResult',
        component: InterviewResultView,
        meta: {
          title: '面试结果 - Sparkie Interview'
        }
      },
      {
        path: 'interview-feedback',
        name: 'InterviewFeedback',
        component: InterviewFeedbackView,
        meta: {
          title: '面试反馈 - Sparkie Interview'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    if (!authStore.isAuthenticated) {
      next('/login')
      return
    }

    // 检查角色权限
    if (to.meta.role && authStore.userRole !== to.meta.role) {
      // 根据用户角色重定向到相应首页
      const redirectPath = authStore.userRole === 'hr' ? '/hr/dashboard' : '/candidate/dashboard'
      next(redirectPath)
      return
    }
  }

  next()
})

export default router 